package itsm.isperp.module.controller.individuation; 

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.BpmProcessSetting;
import itsm.isperp.module.entity.individuation.IddIndividuationForm;
import itsm.isperp.module.service.bpm.BpmProcessSettingService;
import itsm.isperp.module.service.individuation.IddIndividuationFormService;
import itsm.isperp.workflow.graph.ShapeMeta;
import itsm.isperp.workflow.service.BpmProcessDefinitaionService;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-04-07 12:01
 */
@Controller
@RequestMapping("individuation/individuationForm")
public class IddIndividuationFormController extends BaseController<IddIndividuationForm> {

	@Autowired
	protected IddIndividuationFormService iddIndividuationFormService;
	
	@Autowired
  BpmProcessDefinitaionService bpmProcessDefinitaionService;

	@Override
	public IddIndividuationFormService getService() {
		return this.iddIndividuationFormService;
	}
	
	@Override
	public String getPageTitle() {
		return "流程表单定义";
	}
	
	
	@RequestMapping("getPinYin")
  @ResponseBody
  public String getPinYin(JqGridRequest request, HttpServletRequest req) {
	  String src=req.getParameter("src");
	  String result=itsm.isperp.framework.utils.StringUtils.getPinYin(src);
    return result;
  }
	
  @RequestMapping("individuationProcessManage")
  public ModelAndView settingEdit(String categoryId, HttpServletRequest req) {
    ModelAndView mv = new ModelAndView("individuation/individuationForm/individuationProcessManage");
    return mv;
  }
  
  @RequestMapping("individuationProcessEdit")
  public ModelAndView individuationProcessEdit(String categoryId,String id, HttpServletRequest req) {
    ModelAndView mv = new ModelAndView("individuation/individuationForm/individuationProcessEdit");
    
    if(StringUtils.isEmpty(id))
    {
    }else
    {
      BpmProcessSettingService bpmProcessSettingService = ContextHolder.getSpringBean("bpmProcessSettingService");
      BpmProcessSetting bps=bpmProcessSettingService.findOne(id);
      String pk=bps.getActReProcdefKey();
      mv.setViewName("redirect:/bpm/processSetting/iddEdit?processKey="+pk);
    }
    return mv;
  }
  
  @RequestMapping("individuationProcessDefEdit")
  public ModelAndView individuationProcessDefEdit(String actReProcdefKey, HttpServletRequest req) throws IOException, DocumentException {
    ModelAndView mv = new ModelAndView("individuation/individuationForm/individuationProcessDefEdit");
    
    
    ProcessDefinition processDefinition = bpmProcessDefinitaionService
        .getProcessDefinitionByKey(actReProcdefKey);

    if (processDefinition.getDeploymentId() != null) {

      ShapeMeta shapeMeta = bpmProcessDefinitaionService
          .getProcessDefinitionShapeMeta(processDefinition);

      mv.addObject("shapeMeta", shapeMeta);
      mv.addObject("processDefinition", processDefinition);
      String modelId = this.bpmProcessDefinitaionService
          .getModelByDeploymentId(
              processDefinition.getDeploymentId()).getId();
      mv.addObject("modelId", modelId);

    }
    return mv;
  }
  
  
	
}
